package data
{
	/**
	 * Use this class to consume the Twitter JSON service. 
	 */	
	import com.adobe.serialization.json.JSON;
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	public class LoadTweetService extends EventDispatcher
	{
		public static const JSON_LOADED 	: String = "json.Loaded";

		private var _url					: String = "";
		private var _loader					: URLLoader;
		private var _data					: Object;
		
		public function LoadTweetService()
		{
			// ...
		}
		
		/**
		 * Main method that goes out and grab the JSON
		 * data.
		 * 
		 * @param $url : String
		 */		
		public function init($url:String) : void
		{
			this._url = $url;
			var myRequest : URLRequest = new URLRequest(_url);
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, _onLoad);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, _onIOError);
			_loader.load(myRequest);
		}
		
		/**
		 * Event processes the xml then dispatches a class event
		 * to let the listener of this class know that the data
		 * is ready.
		 * 
		 * @param $e : Event
		 */		
		private function _onLoad($e : Event) : void
		{
			_data = JSON.decode(_loader.data);
			dispatchEvent(new Event(LoadTweetService.JSON_LOADED));
		}
		
		private function _onIOError($e : IOErrorEvent) : void
		{
			trace ("Data did not load");
		}

		/**
		 * Return JSON data.
		 * @return Object 
		 * 
		 */		
		public function get data() : Object
		{
			return _data;
		}
	}
}